home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / communic / pcmail / main / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  7.4 KB  |  257 lines

  1. /*++
  2. /* NAME
  3. /*      file 3
  4. /* SUMMARY
  5. /*      manipulate ordinary files
  6. /* PROJECT
  7. /*      pc-mail
  8. /* PACKAGE
  9. /*      mail
  10. /* SYNOPSIS
  11. /*      int file()
  12. /*
  13. /*    int junk_file()
  14. /* DESCRIPTION
  15. /*      file() is called when the user selected the "file" option
  16. /*    in the main menu. The pager displays a directory listing on the
  17. /*      screen. The user may then walk through the file display and
  18. /*    select a file or directory. 
  19. /*
  20. /*    In case a regular file is selected the user may specify
  21. /*    an action (send file as mail, delete file, display
  22. /*    file, print file, copy file to workfile for subsequent
  23. /*    editing etc.).
  24. /*
  25. /*    junk_file() should be invoked when the file display is no longer
  26. /*    valid.
  27. /* FILES
  28. /*      By selecting directory files the user can walk through the whole
  29. /*      file system.
  30. /* SEE ALSO
  31. /*      kbdinp(3), pager(3)
  32. /* BUGS
  33. /*    MS-DOS has no "." or ".." entries in the root directory, and
  34. /*    subdirectories of an MS-DOS root directory have no ".." entry.
  35. /*
  36. /*    Changing the current directory may cause problems if
  37. /*    environment variables are defined in terms of relative path names.
  38. /*
  39. /*    There is no provision to change the "default drive" on systems
  40. /*    that use such abominations.
  41. /* AUTHOR(S)
  42. /*      W.Z. Venema
  43. /*      Eindhoven University of Technology
  44. /*      Department of Mathematics and Computer Science
  45. /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  46. /* CREATION DATE
  47. /*      Sun Apr  5 17:18:06 GMT+1:00 1987
  48. /* LAST MODIFICATION
  49. /*    90/01/22 13:01:37
  50. /* VERSION/RELEASE
  51. /*    2.1
  52. /*--*/
  53.  
  54. #include <stdio.h>
  55. #include <time.h>
  56. #include <sys/types.h>
  57. #include <sys/stat.h>
  58. #include <ctype.h>
  59.  
  60. #include "defs.h"
  61. #include "screen.h"
  62. #include "pager.h"
  63. #include "mail.h"
  64. #include "ndir.h"
  65. #include "path.h"
  66. #include "status.h"
  67. #include "window.h"
  68.  
  69. extern struct tm *localtime();        /* std C library */
  70.  
  71. hidden File *bldlist();            /* forward declaration */
  72. hidden int show_list();
  73. hidden int pick_file();
  74. hidden int delfile();
  75. hidden int show_file();
  76. hidden int rmfile();
  77.  
  78. hidden File *dfile = 0;            /* file listing */
  79. hidden File *dirfile = 0;        /* directory listing */
  80. hidden char file_prompt[BUFSIZ];    /* prompt with directory name */
  81. hidden char file_dir[BUFSIZ];        /* prompt with directory name */
  82. hidden char prompt_fmt[] = "To display a file, select it with \
  83. the cursor keys, then press ENTER\n(showing directory: \"%s\")";
  84.  
  85. /* file - called from keyboard interpreter, call keyboard driver */
  86.  
  87. public int file()
  88. {
  89.     char *getcwd();
  90.     char  thisdir[BUFSIZ];
  91.  
  92.     static Screen screen[] = {
  93.     'C',    "Close",0,    prevscreen,
  94.     'P',    "Print",print,    "Print file listing",
  95.     'S',    "Save",    save,    "Save file listing to ordinary file",
  96.     PGUP,    PgUp,    pu_pager,pageup,
  97.     PGDN,    PgDn,    pd_pager,pagedn,
  98.     UP,    "Up",    up_pager,csrup,
  99.     DOWN,    "Down",    dn_pager,csrdn,
  100.     ENTER,    "Enter",pick_file,"Display file at cursor",
  101.     0,    0,    show_list,
  102.     file_prompt,
  103.     };
  104.  
  105.     (void) getcwd(thisdir, sizeof(thisdir));    /* save current directory */
  106.     sprintf(file_prompt, prompt_fmt, thisdir);    /* put cwd in prompt */
  107.     kbdinp(screen);                /* set up dialogue */
  108.     close_pager(dirfile);            /* deallocate pager file */
  109.     dirfile = 0;                /* say it's gone */
  110.     (void) chdir(thisdir);            /* restore current directory */
  111.     return (S_REDRAW);                /* force screen update */
  112. }
  113.  
  114. /* show_list - create or refresh file listing */
  115.  
  116. hidden int show_list()
  117. {
  118.     if (dirfile == 0) {                /* no pager file? */
  119.     dirfile = bldlist();            /* create one */
  120.     } else {
  121.     set_pager(dirfile);            /* select existing pager file */
  122.     }
  123.     ds_pager();                    /* display the thing */
  124.     return (0);                    /* say screen is up-to-date */
  125. }
  126.  
  127. /* bldlist - build file listing display */
  128.  
  129. hidden File *bldlist()
  130. {
  131.     register File *pp;
  132.     register DIR *dp;
  133.     register struct direct *de;
  134.     struct stat s;
  135.  
  136.     patience();
  137.  
  138.     if ((dp = opendir(THISDIR)) == 0) {
  139.     fatal("insufficient memory for operation");
  140.     /* NOTREACHED */
  141.     } else {
  142.     pp = open_pager();            /* allocate pager file */
  143.  
  144.     while (de = readdir(dp)) {        /* read next file name */
  145.         if (stat(de->d_name, &s) == 0) {    /* get file info */
  146.         if ((s.st_mode & S_IFMT) == S_IFDIR)
  147.             app_pager(pp, strcons("%-20s %9s %s", de->d_name,
  148.                       "<dir>", tstamp(&s.st_mtime)));
  149.         else
  150.             app_pager(pp, strcons("%-20s %9ld %s", de->d_name,
  151.                       s.st_size, tstamp(&s.st_mtime)));
  152.         }
  153.     }
  154.     sort_pager(pp, FORW_SORT);        /* sort by file name */
  155.  
  156.     (void) getcwd(file_dir, sizeof(file_dir));
  157.     sprintf(file_prompt, prompt_fmt, file_dir);    /* put cwd in prompt */
  158.  
  159.     closedir(dp);                /* directory search done */
  160.     return (pp);                /* current pager file */
  161.     }
  162. }
  163. /* pick_file - display selected file or change directory */
  164.  
  165. hidden int pick_file()
  166. {
  167.     static char botline[80];
  168.     static Screen screen[] = {
  169.     'C',    "Close",0,        "Return to file listing",
  170.     'D',    "Delete",delfile,    "Delete this file",
  171.     'M',    "Mail",    mailfile,    "Mail a copy of this file",
  172.     'P',    "Print",print,        "Print this file",
  173.     'S',    "Save",    save,        "Save a copy to an ordinary file",
  174.     'W',    "Work",    makework,    "Save a copy to a work file",
  175.     '|',    "|",    filter,        "Filter file through command",
  176.     PGUP,    PgUp,    pu_pager,    pageup,
  177.     PGDN,    PgDn,    pd_pager,    pagedn,
  178.     UP,    "Up",    up_pager,    csrup,
  179.     DOWN,    "Down",    dn_pager,    csrdn,
  180.     0,    0,    show_file,
  181.     botline,
  182.     };
  183.     struct stat s;
  184.  
  185.     if (!sscanf(gets_pager(), "%s", message)) {    /* cannot read name */
  186.     beep();                    /* "notify" user */
  187.     return (0);                /* nothing happened */
  188.     } else if (access(message, 04) 
  189.     || stat(message, &s)) {            /* cannot read file */
  190.     beep();                    /* "notify" user */
  191.     return (0);                /* nothing happened */
  192.     } else if ((s.st_mode & S_IFMT) == S_IFDIR) {    /* directory file */
  193.     chdir(message);                /* change directory */
  194.     close_pager(dirfile);            /* purge display */
  195.     dirfile = bldlist();            /* new file display */
  196.     return (S_REDRAW);            /* update display */
  197.     } else {                    /* ordinary file */
  198.     sprintf(botline, "(Reading file \"%s\")", message);
  199.     kbdinp(screen);                /* look at screen */
  200.     close_pager(dfile);            /* and forget it */
  201.     dfile = 0;                /* say it's gone */
  202.     return (S_REDRAW);            /* force redrawing */
  203.     }
  204. }
  205.  
  206. /* show_file - create or refresh display of selected file */
  207.  
  208. hidden int show_file()
  209. {
  210.     if (dfile) {
  211.     set_pager(dfile);            /* select dispay */
  212.     } else {
  213.     if (rd_pager(dfile = open_pager(),
  214.              message))            /* try to read file */
  215.         mesg_pager(dfile, m_msgread);    /* read error */
  216.     }
  217.     ds_pager();                    /* rewrite screen */
  218.     return (0);                    /* screen up-to-date */
  219. }
  220.  
  221. /* delfile - ask confirmation before removing a file */
  222.  
  223. hidden int delfile()
  224. {
  225.     static Screen screen[] = {
  226.     ESCCR, 0, rmfile, int_error,
  227.     0, 0, 0,
  228.     "Press ESC to cancel. Confirm with ENTER",
  229.     };
  230.  
  231.     return (kbdinp(screen) | S_REDRAW);        /* "are you sure?" */
  232. }
  233.  
  234. /* rmfile - actually remove file */
  235.  
  236. hidden int rmfile()
  237. {
  238.     if (unlink(message)) {            /* try to remove file */
  239.     errdisp(E_UNLINK);            /* notify the user */
  240.     return (S_BREAK | S_REDRAW);        /* redisplay, stop caller */
  241.     } else {
  242.     junk_file();                /* say file display outdated */
  243.     return (S_BREAK);            /* terminate caller */
  244.     }
  245. }
  246.  
  247. /* junk_file - directory listing is no longer up to date */
  248.  
  249. public int junk_file()
  250. {
  251.     if (dirfile) {
  252.     close_pager(dirfile);            /* close pager file */
  253.     dirfile = 0;                /* say it is gone */
  254.     }
  255.     return (0);                    /* in case we need that */
  256. }
  257.